//
//  VistorView.swift
//  WeiBo09
//
//  Created by 裘明 on 15/9/2.
//  Copyright © 2015年 qm. All rights reserved.
//

import UIKit

class VistorLoginView: UIView {
    
    /// 设置访客视图的图片和标签
    ///
    /// - parameter imageName: 图片名
    /// - parameter message:   标签名
    func setupinfo(imageName:String?, message:String) {
        
        messageLabel.text = message
        
        if let imgName = imageName {
            
            iconView.image = UIImage(named: imgName)
            // 隐藏图片
            homeiconView.hidden = true
            
            // 将图标移至图层底部
            sendSubviewToBack(maskIconView)
                        
        } else {
            
            startAnimation()
        }
        
    }
        /// 图片动画效果
        private func startAnimation() {
       
            let anim = CABasicAnimation(keyPath: "transform.rotation")
            anim.duration = 6
            anim.toValue = M_PI
            anim.repeatCount = MAXFLOAT
            anim.removedOnCompletion = false
            
            iconView.layer.addAnimation(anim, forKey: nil)
        }
        
    

    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI ()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    /// 设置界面 
    private func setupUI() {
        
        // 1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeiconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 2.添加约束
        // 2.1图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        // 2.2图片
        homeiconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeiconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeiconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 2.3提示文字
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant:16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        // 2.4注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        // 2.5登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 2.6添加遮罩
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
    addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":maskIconView]))
     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[h]-(-35)-[m]", options: [], metrics: nil, views: ["h":maskIconView, "m":loginBtn]))
        
        backgroundColor = UIColor(white: 236.0/255.0, alpha: 1)
    }
    
    // 1.图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 2.图片
    private lazy var homeiconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 6.添加遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 3.提示文字
    private lazy var messageLabel: UILabel = {

        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.grayColor()
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        return label
    }()
    
    // 4.注册按钮
     lazy var registerBtn: UIButton = {
        let butten = UIButton()
       
        butten.setTitle("注册", forState: UIControlState.Normal)
        butten.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        butten.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return butten
    }()
    
    // 5.添加登录按钮
     lazy var loginBtn: UIButton = {
       let butten = UIButton()
        
        butten.setTitle("登录", forState: UIControlState.Normal)
        butten.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        butten.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return butten
    }()
    
    



}

